package com.czh.auth.application.controller;

import com.alibaba.fastjson2.JSON;
import com.czh.auth.application.convert.AuthRoleConverter;
import com.czh.auth.application.dto.AuthRoleDTO;
import com.czh.auth.api.entity.ResponseResult;
import com.czh.auth.domain.entity.AuthRoleBO;
import com.czh.auth.domain.service.AuthRoleDomainService;
import com.google.common.base.Preconditions;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色controller
 * @author caozaihui
 * @since  2024/8/28 18:28
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     * @param authRoleDTO
     * @return Boolean
     * @since 2024/8/28 下午 6:36
     */
    @PostMapping("add")
    public ResponseResult<Boolean> addRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convert(authRoleDTO);
            return ResponseResult.success(authRoleDomainService.addRole(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.add.error:{}", e.getMessage(), e);
            return ResponseResult.error("创建角色失败!");
        }
    }
    /** 
     * 修改权限
     * @param authRoleDTO 
     * @return bool
     * @since 2024/8/28 下午 6:46
     */ 
    @PostMapping("update")
    public ResponseResult<Boolean> updateRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.updateRole.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convert(authRoleDTO);
            return ResponseResult.success(authRoleDomainService.updateRole(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.updateRole.error:{}", e.getMessage(), e);
            return ResponseResult.error("修改角色失败!");
        }
    }
    @PostMapping("delete")
    public ResponseResult<Boolean> deleteRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.deleteRole.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            return ResponseResult.success(authRoleDomainService.deleteRole(authRoleDTO.getId()));
        } catch (Exception e) {
            log.error("RoleController.deleteRole.error:{}", e.getMessage(), e);
            return ResponseResult.error("删除角色失败!");
        }
    }
}
